using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class MarterialReturnDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public MarterialReturnDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the MarterialReturn table.
		/// </summary>
		public void Insert(MarterialReturnDTO marterialReturn)
		{
			ValidationUtility.ValidateArgument("marterialReturn", marterialReturn);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderFormMaterial", marterialReturn.IdOrderFormMaterial),
				new SqlParameter("@ReturnDay", marterialReturn.ReturnDay),
				new SqlParameter("@Amount", marterialReturn.Amount)
			};

			marterialReturn.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "MarterialReturnInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the MarterialReturn table.
		/// </summary>
		public void Update(MarterialReturnDTO marterialReturn)
		{
			ValidationUtility.ValidateArgument("marterialReturn", marterialReturn);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", marterialReturn.Id),
				new SqlParameter("@IdOrderFormMaterial", marterialReturn.IdOrderFormMaterial),
				new SqlParameter("@ReturnDay", marterialReturn.ReturnDay),
				new SqlParameter("@Amount", marterialReturn.Amount)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MarterialReturnUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the MarterialReturn table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MarterialReturnDelete", parameters);
		}

		/// <summary>
		/// Deletes a record from the MarterialReturn table by a foreign key.
		/// </summary>
		public void DeleteAllByIdOrderFormMaterial(int idOrderFormMaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderFormMaterial", idOrderFormMaterial)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "saMarterialReturnDeleteAllByIdOrderFormMaterial", parameters);
		}

		/// <summary>
		/// Selects a single record from the MarterialReturn table.
		/// </summary>
		public MarterialReturnDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MarterialReturnSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the MarterialReturn table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "MarterialReturnSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the MarterialReturn table.
		/// </summary>
		public List<MarterialReturnDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MarterialReturnSelectAll"))
			{
				List<MarterialReturnDTO> marterialReturnDTOList = new List<MarterialReturnDTO>();
				while (dataReader.Read())
				{
					MarterialReturnDTO marterialReturnDTO = MapDataReader(dataReader);
					marterialReturnDTOList.Add(marterialReturnDTO);
				}

				return marterialReturnDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the MarterialReturn table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "MarterialReturnSelectAll");
		}

		/// <summary>
		/// Selects all records from the MarterialReturn table by a foreign key.
		/// </summary>
		public List<MarterialReturnDTO> SelectAllByIdOrderFormMaterial(int idOrderFormMaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderFormMaterial", idOrderFormMaterial)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "saMarterialReturnSelectAllByIdOrderFormMaterial", parameters))
			{
				List<MarterialReturnDTO> marterialReturnDTOList = new List<MarterialReturnDTO>();
				while (dataReader.Read())
				{
					MarterialReturnDTO marterialReturnDTO = MapDataReader(dataReader);
					marterialReturnDTOList.Add(marterialReturnDTO);
				}

				return marterialReturnDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the MarterialReturn table by a foreign key.
		/// </summary>
		public string SelectAllByIdOrderFormMaterialJson(int idOrderFormMaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdOrderFormMaterial", idOrderFormMaterial)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "saMarterialReturnSelectAllByIdOrderFormMaterial", parameters);
		}

		/// <summary>
		/// Creates a new instance of the MarterialReturnDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private MarterialReturnDTO MapDataReader(SqlDataReader dataReader)
		{
			MarterialReturnDTO marterialReturnDTO = new MarterialReturnDTO();
			marterialReturnDTO.Id = dataReader.GetInt32("Id", 0);
			marterialReturnDTO.IdOrderFormMaterial = dataReader.GetInt32("IdOrderFormMaterial", 0);
			marterialReturnDTO.ReturnDay = dataReader.GetDateTime("ReturnDay", new DateTime(0));
			marterialReturnDTO.Amount = dataReader.GetInt32("Amount", 0);

			return marterialReturnDTO;
		}

		#endregion
	}
}
